package cn.yunhe.dao.impl;

import cn.yunhe.dao.UserDao;
import cn.yunhe.model.User;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDaoimpl implements UserDao {
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
        return namedParameterJdbcTemplate;
    }

    public void setNamedParameterJdbcTemplate(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
        this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
    }

    @Override
    public User findById(int id) {
        StringBuilder sql=new StringBuilder();
        sql.append("select* from users where id=:id");
        Map paraMap=new HashMap();
        paraMap.put("id",id);
        List<User> userList =namedParameterJdbcTemplate.query(sql.toString(),paraMap,rowMapper);
        return userList.size()>0?userList.get(0):null;
    }

    @Override
    public List<User> searchUser() {
        StringBuilder sql=new StringBuilder();
        sql.append("select* from users");
        List<User>userList=namedParameterJdbcTemplate.query(sql.toString(),new HashMap(),rowMapper);
        return userList;
    }

    @Override
    public List<User> searchUser(User user) {
        StringBuilder sql=new StringBuilder();
        sql.append("select* from users where 1=2 ");

        Map paraMap=new HashMap();
        if (user!=null){
            if (user.getUname()!=null&&user.getUname().trim().length()>0){
                sql.append("or uname like :uname ");
                paraMap.put("uname","%"+user.getUname()+"%");
            }
        }
        List<User>userList=namedParameterJdbcTemplate.query(sql.toString(),paraMap,rowMapper);
        return userList;
    }

    @Override
    public boolean insertUser(User user) {
        StringBuilder sql=new StringBuilder();
        sql.append("insert into users(uname,upwd) ");
        sql.append("values(:uname,:upwd) ");
        System.out.println(sql);

        Map paramMap=new HashMap();
        paramMap.put("uname",user.getUname());
        paramMap.put("upwd",user.getUpwd());

        int rowNum= namedParameterJdbcTemplate.update(sql.toString(),paramMap);
        boolean flag=false;
        if (rowNum>0){
            flag=true;
        }
        return flag;
    }

    @Override
    public boolean updateUser(User user) {
        StringBuilder sql=new StringBuilder();
        sql.append("update users set id =id");

        Map paramMap=new HashMap();
        if (user!=null){
            sql.append(", ");
            if (user.getUname()!=null&&user.getUname().trim().length()>0){
                sql.append("uname=:uname, ");
                paramMap.put("uname",user.getUname());
            }
            if (user.getUpwd()!=null&&user.getUpwd().trim().length()>0){
                sql.append("upwd=:upwd ");
                paramMap.put("upwd",user.getUpwd());
            }
            if (1==1){
                sql.append("where id=:id ");
                paramMap.put("id",user.getId());
            }
        }

        System.out.println(sql);

        int rowNum=namedParameterJdbcTemplate.update(sql.toString(),paramMap);
        boolean flag=false;
        if (rowNum>0){
            flag=true;
        }
        return flag;
    }

    @Override
    public boolean deleteUser(User user) {
        StringBuilder sql=new StringBuilder();
        sql.append("delete from users where 1=2 ");

        Map paraMap=new HashMap();
        if (user!=null){
            if (user.getId()>0){
                sql.append("or id=:id ");
                paraMap.put("id",user.getId());
            }
        }
        System.out.println(sql);

        int rowNum= namedParameterJdbcTemplate.update(sql.toString(),paraMap);
        boolean flag=false;
        if (rowNum>0){
            flag=true;
        }
        return flag;
    }
}
